package com.ls.sync;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SyncLockDemo {
    public static void main(String[] args) {
        Object o = new Object();
        Lock lock = new ReentrantLock();

        //sync
      /*  new Thread(()->{
            synchronized (o){
                System.out.println(Thread.currentThread().getName()+" 外层");
                synchronized (o){
                    System.out.println(Thread.currentThread().getName()+" 中层");
                    synchronized (o){
                        System.out.println(Thread.currentThread().getName()+" 内层");
                    }
                }
            }
        },"aa").start();*/

        //lock
        new Thread(() -> {
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName() + " 外层");
                try {
                    lock.lock();
                    System.out.println(Thread.currentThread().getName() + " 内层");

                } finally {
                    //如果不释放锁,别的线程就出现问题了
                    lock.unlock();
                }
            } finally {
                lock.unlock();
            }

        }, "aa").start();
        new Thread(() -> {
            lock.lock();
            System.out.println(111);
            lock.unlock();
        },"bb").start();
    }
}
